package cool.taomu.box.kademlia.test

import cool.taomu.toolkit.service.kademlia.service.KRoutingTable
import org.junit.jupiter.api.Test
import cool.taomu.toolkit.utils.PeerUtils
import static org.junit.Assert.assertEquals

class RoutingTableTest {

    @Test
    def void xorDistanceTest() {
        val id1 = "1011100100011001100010111001110000001011011010111110111001100101001100001011111110110101110111101010000011101001110000010010101010011000101100011111101011001"
        val id2 = "100001001110110110010001001001101000010100010111110011111001110111011100101111010110111101001011001000010111111100110100000110010100100110110111111100001011110"

        var id = "GET:/kademlia/index2/002004"
        var key160 = PeerUtils.stringToHash(id);
        var result1 = PeerUtils.xorDistance(id1, key160)
        assertEquals(result1.bitCount, 134)

        var result2 = PeerUtils.xorDistance(id2, key160)
        assertEquals(result2.bitCount, 133)

        id = "GET:/kademlia/index2/002008"
        key160 = PeerUtils.stringToHash(id);
        result1 = PeerUtils.xorDistance(id1, key160)
        assertEquals(result1.bitCount, 123)

        result2 = PeerUtils.xorDistance(id2, key160)
        assertEquals(result2.bitCount, 124)

    }

    @Test
    def void stringTo160BitTest() {
        val id1 = "1101"
        // 第一次会计算并缓存
        val result1 = PeerUtils.stringToHash(id1)
        // 第二次从缓存中取
        val result2 = PeerUtils.stringToHash(id1)
        assertEquals(result1, result2)

    }
}
